package com.cx.controller.wjr;

import com.cx.pojo.Page;
import com.cx.pojo.PageExample;
import com.cx.pojo.wjr.PriceReturn;
import com.cx.pojo.wjr.UserPage;
import com.cx.service.wjr.UserService;
import com.cx.util.wjr.GetDistance;
import com.cx.util.wjr.IOxlsl;
import com.cx.util.wjr.PriceSelect;
import com.cx.vo.ResponseResult;
import com.cx.vo.wjr.Receive;
import com.jptangchina.jxcel.JxcelParser;

import org.apache.tomcat.util.http.fileupload.FileUploadBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserPageController {
    @Autowired
    UserService userService;

    @ExceptionHandler(MultipartException.class)
    public ResponseResult uploadExcepttion(MultipartException e){
        System.out.println("发生异常");
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(1);
        responseResult.setMessage("上传失败");
        return responseResult;
    }

    @RequestMapping("addUserPage")
    @ResponseBody
    public ResponseResult add(Model model, UserPage user){
        ResponseResult responseResult = new ResponseResult();
        System.out.println(user);
        String rdSpeed = null;
        rdSpeed = user.getRdSpeed().replace(",","");
        StringBuffer stringBuffer = new StringBuffer();
        String start = user.getSendProvince() + user.getSendCity() + user.getSendArea() + user.getSendDetail();
        String end = user.getReceiveProvince() + user.getReceiveCity();
        int money = PriceSelect.selectPrice(start,end,rdSpeed);
        String startLonLat = GetDistance.getLonLat(start);
        String[] location = startLonLat.split(",");
        user.setLongitude(location[0]);
        user.setLatitude(location[1]);
        if(null != user.getRdSpeed()){
            if(null != user.getRemarksTip()){
                stringBuffer.append(user.getRemarksTip());
            }
            if(null != user.getRemarkText()){
                stringBuffer.append(user.getRemarkText());
            }
            user.setRemark(String.valueOf(stringBuffer));
            System.out.println(user);
            System.out.println(user.getRemark());
            user.setMoney(String.valueOf(money));
            rdSpeed = user.getRdSpeed().replace(",","");
            Integer w = Integer.valueOf(rdSpeed);
            user.setWeight(String.valueOf(w));
            user.setStatus("已下单");
            try{
                int count = userService.addUserPage(user);
            }catch (Exception e){
                e.printStackTrace();
                responseResult.setMessage("添加失败");
                responseResult.setCode(2);
                return responseResult;
            }
            responseResult.setMessage("添加成功");
            responseResult.setCode(1);
            return responseResult;
        }
        System.out.println(user);
        responseResult.setCode(2);
        responseResult.setMessage("数据请求异常");
        return responseResult;
    }


    @RequestMapping("userSelectByPhone")
    @ResponseBody
    public List<Page> selectByPhone(Model model, UserPage userpage){
        PageExample pageExample = new PageExample();
        List<Page> list = userService.selectByPagePhone(pageExample);
        return list;
    }

    @RequestMapping("userSelectByNum")
    @ResponseBody
    public List<Page> selectByPageNum(Model model, String pageNums){
        PageExample pageExample = new PageExample();
        pageExample.createCriteria().andPageNumEqualTo(Integer.valueOf(pageNums));
        List<Page> list = userService.selectByPageNum(pageExample);
        return list;
    }

    @RequestMapping("batchAddUserPageAJAX")
    @ResponseBody
    public ResponseResult batchAddAJAX(UserPage userPage,@RequestParam("file") MultipartFile file, ResponseResult responseResult, Model model, UserPage user){
        System.out.println(userPage);
        userPage.setPageNum(123);
        if (!file.isEmpty()) {
            System.out.println("文件上传成功");
        }else{
            responseResult.setMessage("文件上传失败");
            return responseResult;
        }
        List<Receive> receive = null;
        List<UserPage> userPageList = new ArrayList<>();
        try {
            File sb = IOxlsl.file(file);
            receive = JxcelParser.parser().parseFromFile(Receive.class,sb);
            System.out.println(receive);
            for (Receive r: receive) {
                if(r.isNull()){

                    String start = r.getReceiveProvince() + r.getReceiveCity();
                    int money = 1;
                    try{
                        money = PriceSelect.selectPrice(start,r.getWeight());
                        System.out.println("money="+money);
                    }catch (Exception e){
                        e.printStackTrace();
                        responseResult.setCode(4);
                        responseResult.setMessage("无效的地址信息");
                        return responseResult;
                    }

                    UserPage userPageAdd = new UserPage();
                    userPageAdd.setSendName(userPage.getSendName());
                    String userReceivePhone = r.getReceivePhone().replace(",","");
                    System.out.println(userReceivePhone);
                    userPageAdd.setSendPhone(user.getSendPhone());
                    userPageAdd.setSendCity(user.getSendCity());
                    userPageAdd.setSendProvince(user.getSendProvince());
                    userPageAdd.setSendArea(user.getSendArea());
                    userPageAdd.setSendDetail(user.getSendAddress());
                    userPageAdd.setPageType(r.getPageType());
                    userPageAdd.setReceivePhone(userReceivePhone);
                    userPageAdd.setReceiveAddress(r.getReceiveDetail());
                    userPageAdd.setReceiveArea(r.getReceiveArea());
                    userPageAdd.setReceiveCity(r.getReceiveCity());
                    userPageAdd.setReceiveProvince(r.getReceiveProvince());

                    userPageAdd.setReceiveName(r.getReceiveName());
                    userPageAdd.setWeight(r.getWeight());
                    userPageList.add(userPageAdd);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setCode(2);

            return responseResult;
        }
        System.out.println(userPageList);
        responseResult.setCode(1);
        responseResult.setData(userPageList);
        responseResult.setMessage(userPageList.size()+"");
        return responseResult;


    }

    @RequestMapping("batchAddUserPage")
    @ResponseBody
    public ResponseResult batchAdd(UserPage userPage,@RequestParam("file") MultipartFile file, ResponseResult responseResult, Model model, UserPage user){
        String rdSpeed = null;
        if (!file.isEmpty()) {
            System.out.println("文件上传成功");
        }else{
            responseResult.setMessage("文件上传失败");
            return responseResult;
        }
        List<Receive> receive = null;
        List<UserPage> userPageList = new ArrayList<>();
        try {
            File sb = IOxlsl.file(file);
            receive = JxcelParser.parser().parseFromFile(Receive.class,sb);
            for (Receive r: receive) {
                if(r.isNull()){
                    UserPage userPageAdd = new UserPage();
                    String start = r.getReceiveProvince() + r.getReceiveCity();
                    String end = user.getSendProvince() + user.getSendCity();

                    String startLonLat = GetDistance.getLonLat(start);
                    String[] location = startLonLat.split(",");
                    userPageAdd.setLongitude(location[0]);
                    userPageAdd.setLatitude(location[1]);

                    int money = 1;
                    try{
                        money = PriceSelect.selectPrice(start,end,r.getWeight());
                    }catch (Exception e){
                        responseResult.setCode(4);
                        responseResult.setMessage("无效的地址信息");
                        return responseResult;
                    }

                    int pageCode = (int) (System.currentTimeMillis()%100000);

                    System.out.println(money);
                    userPageAdd.setPageCode(String.valueOf(pageCode));
                    //userPageAdd.setPageNum(pageNum);
                    userPageAdd.setMoney(String.valueOf(money));
                    userPageAdd.setStatus("已下单");
                    String userReceivePhone = r.getReceivePhone().replace(",","");
                    userPageAdd.setSendName(userPage.getSendName());
                    userPageAdd.setSendPhone(user.getSendPhone());
                    userPageAdd.setSendCity(user.getSendCity());
                    userPageAdd.setSendProvince(user.getSendProvince());
                    userPageAdd.setSendArea(user.getSendArea());
                    userPageAdd.setSendDetail(user.getSendAddress());
                    userPageAdd.setSendAddress(user.getSendAddress());
                    userPageAdd.setPageType(r.getPageType());
                    userPageAdd.setReceivePhone(userReceivePhone);
                    userPageAdd.setReceiveAddress(r.getReceiveDetail());
                    userPageAdd.setReceiveArea(r.getReceiveArea());
                    userPageAdd.setReceiveCity(r.getReceiveCity());
                    userPageAdd.setReceiveProvince(r.getReceiveProvince());
                    userPageAdd.setReceiveName(r.getReceiveName());
                    userPageAdd.setUserId(user.getUserId());
                    userPageAdd.setWeight(r.getWeight());

                    userPageList.add(userPageAdd);

                }
            }
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setCode(2);
            responseResult.setMessage("文件格式不符合要求");
            return responseResult;
        }

        System.out.println(userPageList);


        for (UserPage u:userPageList) {
            try{
                int count = userService.addUserPage(u);
            }catch (Exception e){
                responseResult.setMessage("添加失败");
                responseResult.setCode(2);
            }
        }

        responseResult.setCode(1);
        responseResult.setData(userPageList);
        responseResult.setMessage(userPageList.size()+"");

        return responseResult;
    }
}
